<?php

/**
 * @name: HOTN AnimeList
 * @author: HammerOnTheNail
 * @link: http://code.google.com/p/hotnanimelist/
 */

// Alap dolgok bekérése
include("../../class2.php");
include(e_PLUGIN."hotn_animelist/Languages/".e_LANGUAGE.".php");
include(e_PLUGIN."hotn_animelist/Languages/".e_LANGUAGE."_genre.php");
include(e_PLUGIN."hotn_animelist/Class/animelist_mysql.php");
include(e_PLUGIN."hotn_animelist/Class/animelist_project.php");
include(e_PLUGIN."hotn_animelist/Class/animelist_options.php");

//beállítások ellenőrzése:
if ($HOTN_ANIMELIST_OPTIONS['forum_type'] <> "" || $HOTN_ANIMELIST_OPTIONS['download_type']<>"")
{
	include(e_PLUGIN."hotn_animelist/Class/forum_".$HOTN_ANIMELIST_OPTIONS['forum_type'].".php");
	$HOTN_ANIMELIST_FORUM = new hotn_animelist_forum();

	include(e_PLUGIN."hotn_animelist/Class/download_".$HOTN_ANIMELIST_OPTIONS['download_type'].".php");
	$HOTN_ANIMELIST_DOWNLOAD = new hotn_animelist_download();
}

// Theme file
include(e_PLUGIN."hotn_animelist/Class/hotn_animelist_shortcodes.php");
if (file_exists(THEME."hotn_animelist/hotn_animelist_projectlist_template.php"))
	include(THEME."hotn_animelist/hotn_animelist_projectlist_template.php");
else
	include(e_PLUGIN."hotn_animelist/Class/hotn_animelist_projectlist_template.php");

// hotn_animelist css file beolvasása
if (file_exists(THEME."hotn_animelist/animelist.css")) $eplug_css[] = THEME."hotn_animelist/animelist.css";
else $eplug_css[] = e_PLUGIN_ABS."hotn_animelist/Class/animelist.css";

// Keresés icon beolvasása
if (file_exists(THEME."hotn_animelist/search_show.png")) $HOTN_SEARCH_SHOW = THEME."hotn_animelist/search_show.png";
else $HOTN_SEARCH_SHOW = e_PLUGIN_ABS."hotn_animelist/Images/search_show.png";

// hotn_animelist js file beolvasása
$footer_js[] = e_PLUGIN_ABS."hotn_animelist/Class/js/jquery.js";
$footer_js[] = e_PLUGIN_ABS."hotn_animelist/Class/js/lightbox.js";
$footer_js[] = e_PLUGIN_ABS."hotn_animelist/Class/js/script_list.js";
	
// Oldal címe
define("e_PAGETITLE",HOTN_ANIMELIST_PROJECT_LIST_TITLE);
// Dátum kiíró
$gen = new convert;

//alapból nem jelenik meg a kereső
$show_search = FALSE;
// alap argumentum érték megadása
$arg = "";

// Átadott paraméterek kiolvasása és felbontása, feldolgozása
if(e_QUERY)
{
	$qs = explode(".", e_QUERY);
	$qs['mode'] = $qs[0]; // Státusz
	$qs['type'] = $qs[1]; // Kategória
	$qs['limit'] = $qs[2]; // Mennyiség
	$qs['from'] = $qs[3]; // Nem választható érték, az e107-nek kell
	$qs['order'] = $qs[5]; // Rendezés
	$qs['order_type'] = $qs[6]; // Sorrend
	$qs['search_name'] = $qs[7]; // Keresési kulcsszó
	$qs['genre'] = $qs[4]; // Típus
}

// project állapotának kiválasztása
if (isset($qs['mode']))
{
	$menu_mode[$qs['mode']] = "selected";
	if ($qs['mode']<>"active") $show_search = TRUE;
}
else
{
	$menu_mode['active'] = "selected";
	$qs['mode'] = "active";
}
if ($qs['mode'] <> "all") $arg .= "project_mode = '".$qs['mode']."'";

// megjelenítendő project típus kiválasztása
if (isset($qs['type']))
{
	$menu_type[$qs['type']] = "selected";

	if ($qs['type']<>"all")
	{
		if ($qs['mode'] <> "all") $arg .= " AND ";
		$arg .= "project_type = '".$qs['type']."'";
		$show_search = TRUE;
	}
}
else
{
	$menu_type['all'] = "selected";
	$qs['type'] = "all";
}

//Genre vizsgálata
if (isset($qs['genre']))
{
	foreach (explode("-",$qs['genre']) as $genre)
	{
		$genre_selected[$genre] = 1;
	}
	
	if ($genre_selected[0]<>1 && is_array($genre_selected))
	{
		$genre = array();
		$sql->db_Select("hotn_animelist_genre","project_id","genre_id = ".str_replace("-"," or genre_id = ",$qs['genre'])." GROUP BY project_id","default");
		while ($row = $sql->db_Fetch())
		{
			$genre[] = $row['project_id'];
		}
		
		$arg .= " and (project_id = ".implode(" or project_id = ",$genre).")";
		$show_search = TRUE;
	}
}
else
{
	$genre_selected = array(0=>1);	
}

// Keresési kulcsszó
if (isset($qs['search_name']) && $qs['search_name']<>"")
{
	if ($arg<>"") $arg .= " AND ";
	$arg .= "( project_name LIKE '%".$qs['search_name']."%' or project_eng_name LIKE '%".$qs['search_name']."%' or project_hun_name LIKE '%".$qs['search_name']."%' or project_other_name LIKE '%".$qs['search_name']."%' )";
	$show_search = TRUE;
}


// Megjelenítendő project mennyiségének kiválasztása
if (isset($qs['limit']))
{
	$menu_limit[$qs['limit']] = "selected";
	if ($sq['limit'] <> "all") $qs['limit'] = (int)$qs['limit'];
	if ($qs['limit'] <> "10") $show_search = TRUE;
}
else
{
	$menu_limit[10] = "selected";
	$qs['limit'] = 10;
}

// Rendezés
if (isset($qs['order']) && $qs['order']<>"")
{
	$menu_order[$qs['order']] = "selected";
	if ($qs['order']<>"project_name") $show_search = TRUE;
	$order = mysql_real_escape_string($qs['order']);
}
else
{
	$order = "project_name";
}

// Rendezés sorrendje
if (isset($qs['order_type']) && $qs['order_type']<>"")
{
	$menu_ordertype[$qs['order_type']] = "selected";
	if ($qs['order_type']<>"ASC") $show_search = TRUE;
	$order .= " ".mysql_real_escape_string($qs['order_type']);
}
else
{
	$order .= " ASC";
}

// Aktuális pozició
if (isset($qs['from']))
{
	$qs['from'] = (int)$qs['from'];
}
else
{
	$qs['from'] = 0;
}

// SQL Limit beállítása
if ($qs['limit'] <> "all") $limit = " LIMIT ".$qs['from'].",".$qs['limit'];

// SQL feltétel
if ($arg<>"") $arg = "WHERE ".$arg;

// Maximális project szám lekérése
$project_count = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_count($arg); 

// SQL rendezési elv
$arg = $arg." ORDER BY ".$order;

// Maximum oldal kiszámítása
$project_page_max = $qs['from']+$qs['limit'];
if ( ($project_page_max > $project_count) || ($project_page_max == 0) ) $project_page_max = $project_count; 

// Paramét összerakása
$parms = $project_count.", ".$qs['limit'].", ".$qs['from'].",".e_SELF.'?'.$qs['mode'].".".$qs['type'].".".$qs['limit'].".[FROM].".$qs['genre'].".".$qs['order'].".".$qs['order_type'].".".$qs['search_name'];

// Adatok kiírása
$text_project = "
<input type='hidden' id='page' name='page' value='".$qs['from']."' />
<div class='hotn_animlist_project_list_menu_container'>
<div id='hotn_animelist_project_list_genre' class='hotn_animelist_project_list_genre hotn_animelist_project_list_menu'>
<div class='hotn_animelist_project_list_number'>
	".HOTN_ANIMELIST_PROJECT_SHOW_PROJECT." ".($qs['from']+1)." - ".$project_page_max." ".HOTN_ANIMELIST_PROJECT_COUNT_PROJECT." ".$project_count."
</div>
<div id='hotn_animelist_project_list_genre_show' class='hotn_animelist_project_list_genre_show' ".($show_search?"style='display:none'":"")."><img src='".$HOTN_SEARCH_SHOW."'> Keresés</div>
";

// Genre kiírása
$genre = $HOTN_ANIMELIST_GENRE;
$row = 5;
$text_project .= "
<div id='hotn_animelist_project_list_genre_search' class='hotn_animelist_project_list_genre_search'>
		<div style='clear:both !important; float:none !important; height: 20px;'></div>
		<div class='hotn_animelist_project_list_menu_mode'>
			".HOTN_ANIMELIST_PROJECT_PROJECT_MODE."
			<select id='mode' name='mode' class='tbox'>
				<option value='all' ".$menu_mode['all'].">".HOTN_ANIMELIST_DEFAULT_ALL."</option>
				<option value='active' ".$menu_mode['active'].">".HOTN_ANIMELIST_ANIMETYPE_ACTIVE."</option>
				<option value='finished' ".$menu_mode['finished'].">".HOTN_ANIMELIST_ANIMETYPE_FINISHED."</option>
				<option value='planned' ".$menu_mode['planned'].">".HOTN_ANIMELIST_ANIMETYPE_PLANNED."</option>
				<option value='passive' ".$menu_mode['passive'].">".HOTN_ANIMELIST_ANIMETYPE_PASSIVE."</option>
				<option value='dropped' ".$menu_mode['dropped'].">".HOTN_ANIMELIST_ANIMETYPE_DROPPED."</option>
			</select>
		</div>
		<div class='hotn_animelist_project_list_menu_type'>
			".HOTN_ANIMELIST_PROJECT_PROJECT_TYPE."
			<select id='type' name='type' class='tbox'>
				<option value='all' ".$menu_type['all'].">".HOTN_ANIMELIST_DEFAULT_ALL."</option>
";
foreach ($HOTN_ANIMELIST_PROJECT->get_types() as $key => $value) {
	$text_project .= "<option value='".$key."' ".$menu_type[$key].">".$value."</option>";
}
$text_project .= "
			</select>
		</div>
		<div class='hotn_animelist_project_list_menu_limit'>
			".HOTN_ANIMELIST_PROJECT_PROJECT_LIMIT."
			<select id='limit' name='limit' class='tbox'>
				<option value='10' ".$menu_limit['10'].">10</option>
				<option value='20' ".$menu_limit['20'].">20</option>
				<option value='30' ".$menu_limit['30'].">30</option>
				<option value='40' ".$menu_limit['40'].">40</option>
				<option value='50' ".$menu_limit['50'].">50</option>
				<option value='all' ".$menu_limit['all'].">".HOTN_ANIMELIST_DEFAULT_ALL."</option>
			</select>
		</div>
		<div style='clear:both !important; float:none !important; height: 20px;'></div>
		<div>
			".HOTN_ANIMELIST_PROJECT_SHOW_PROJECT_ORDER."
			<select id='order' name='order' class='tbox'>
				<option value='project_name' ".$menu_order['project_name'].">".HOTN_ANIMELIST_PROJECT_SHOW_PROJECT_ORDER_PROJECT_NAME."</option>
				<option value='project_files' ".$menu_order['project_files'].">".HOTN_ANIMELIST_PROJECT_SHOW_PROJECT_ORDER_PROJECT_FILE."</option>
				<option value='project_length' ".$menu_order['project_length'].">".HOTN_ANIMELIST_PROJECT_SHOW_PROJECT_ORDER_PROJECT_LENGTH."</option>
			</select>
		</div>
		<div>
			".HOTN_ANIMELIST_PROJECT_SHOW_PROJECT_ORDER_TYPE."
			<select id='order_type' name='order_type' class='tbox'>
				<option value='ASC' ".$menu_ordertype['ASC'].">".HOTN_ANIMELIST_ADMIN_DEFAULT_ASC."</option>
				<option value='DESC' ".$menu_ordertype['DESC'].">".HOTN_ANIMELIST_ADMIN_DEFAULT_DESC."</option>
			</select>
		</div>
		<div style='clear:both !important; float:none !important; height: 20px;'></div>
		<div>
			<span>".HOTN_ANIMELIST_PROJECT_LIST_GENRE_SEARCH."</span> <input class='tbox' type='text' id='search_name' value='".$qs[7]."'> 
		</div>
		<div class='hotn_animelist_project_list_menu_submit'>
			<input class='tbox' type='submit' value='".HOTN_ANIMELIST_PROJECT_PROJECT_SEARCH."' onclick='location.href=\"project_list.php?\"+document.getElementById(\"mode\").value+\".\"+document.getElementById(\"type\").value+\".\"+document.getElementById(\"limit\").value+\".0.\"+get_genre()+\".\"+document.getElementById(\"order\").value+\".\"+document.getElementById(\"order_type\").value+\".\"+document.getElementById(\"search_name\").value' />
		</div>
		<div style='clear:both !important; float:none !important; height: 20px;'></div>
	<table width='100%'>
		<tr>";
	foreach ($genre as $key => $value)
	{
		$selected = "";
		if ($genre_selected[$key] == "1") $selected = "checked";
		$text_project .= "
			<td>
				<input class='tbox' type='checkbox' value='".$key."' name='genre' id='genre' ".$selected."/>
				".$value."
			</td>";
		if ( --$row <= 0)
		{
			$row = 5;
			$text_project .= "
		</tr>
		<tr>";
		}
	}
	$text_project .= "
		</tr>
	</table>
</div>";

$text_project .= "
<div style='clear:both !important; float:none !important'></div>
	</div>
</div>
<div class='hotn_animelist_clear'></div>
";

// Project adatok lekérése
$project = $HOTN_ANIMELIST_DOWNLOAD->hotn_animelist_download_get_project($arg,$limit);

// Projectek kiírása
$text_project .= "<table class='hotn_animelist_project_list'>";
foreach ($project as $row)
{
	$types = $HOTN_ANIMELIST_PROJECT->get_types();
	$row['project_type'] = $types[$row['project_type']];
	if ($row['project_length'] == 0) $row['project_length'] = HOTN_ANIMELIST_PROJECT_ACTIVE_PROJECT;
	if ($row['project_date_end'] == 0) $row['project_date_end'] = HOTN_ANIMELIST_PROJECT_ACTIVE_PROJECT;
	else $row['project_date_end'] = $gen->convert_date($row['project_date_start'],"short");
	$row['project_date_start'] = $gen->convert_date($row['project_date_start'],"short");
	
	if ($row['project_text']<>"")
		$row['project_text'] = "<li class='hotn_animelist_project_list_view'>[ <a href='".e_PLUGIN."hotn_animelist/project_view.php?".$row['project_id']."#content'>".HOTN_ANIMELIST_PROJECT_PROJECT_VIEW."</a> ]</li>";

	$text_project .= "
		<tr>
			<td class='hotn_animelist_project_list_image'><a href='project_view.php?".$row['project_id']."#download'><img src='Project/".$row['project_folder']."/".$row['project_image_reference']."' /></a></td>
			<td class='hotn_animelist_project_list_info'>
				<ul>
					<li class='hotn_animelist_project_list_title'><a href='project_view.php?".$row['project_id']."#download'>".$row['project_name']."</a><br/></li>
					<li class='hotn_animelist_project_list_type'><span>".HOTN_ANIMELIST_PROJECT_PROJECT_TYPE."</span>".$row['project_type']."<br/></li>
					<li class='hotn_animelist_project_list_file'><span>".HOTN_ANIMELIST_PROJECT_PROJECT_FILE."</span>".$row['project_files']." / ".$row['project_length']."<br /></li>
					<li class='hotn_animelist_project_list_date_start'><span>".HOTN_ANIMELIST_PROJECT_PROJECT_DATE_START."</span>".$row['project_date_start']."</li>
					<li class='hotn_animelist_project_list_date_end'><span>".HOTN_ANIMELIST_PROJECT_PROJECT_DATE_END."</span>".$row['project_date_end']."</li>
					".$row['project_text']."
					<li class='hotn_animelist_project_list_download'>[ <a class='content_link' href='project_view.php?".$row['project_id']."#download'>" .HOTN_ANIMELIST_PROJECT_PROJECT_DOWNLOAD. "</a> ]</li>
				</ul>
			</td>
		</tr>
	";
}
$text_project .= "</table>";

// Léptető kirajzolása
$text_project .= "<div class='nextprev'>".$tp->parseTemplate("{NEXTPREV={$parms}}")."</div>";

// Oldal megjelenítése
require_once(HEADERF);
$ns -> tablerender(HOTN_ANIMELIST_PROJECT_LIST_TITLE, $text_project, "");
require_once(FOOTERF);
?>